package com.hx.zhg.web.filter;

import com.hx.zhg.exception.BusinessException;
import com.hx.zhg.service.system.WxSystemService;
import com.hx.zhg.service.utils.SpringUtil;
import com.hx.zhg.web.util.RequestUtil;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by zhenghegong on 2020/4/21.
 */
public class WxJsSDKConfigFilter extends BaseFilter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        try {
            WxSystemService wxSystemService = (WxSystemService) SpringUtil.getBean("wxSystemService");
            RequestUtil.setJSSDKConfig(req, wxSystemService.getJSSDKConfig(RequestUtil.getCurrUrl(contextPath, req)));
        } catch (Exception e) {
            throw new BusinessException("设置微信JSSDK_CONFIG异常");
        }
        chain.doFilter(request, response);
    }
}
